/**
 * Created by afei on 17-1-11.
 */
/****
 * 初始化主机
 */
function updateinfo(msg){
    var html_before = "<div class='overlay'><p class='popup'><span>";
    var html_after = "</span><sapn class='fa fa-circle'></sapn></p></div>";
    var data = msg + ' ....'
    $('body').append(html_before + data + html_after);
    // todo 1. 优化初始化进度页面--动画效果
}
$('#host-initialization').click(function(){
    updateinfo('正在初始化');
    $.ajax({
        type: 'POST',
        url: '/host_init',
        data: {"name":"host_init","csrfmiddlewaretoken": jscsrftoken },
        dataType: 'json',
        success: function(msg){ //todo 返回错误信息
            // 弹出浮层确认页面
           var html_before = "<div class='overlay'><p class='popup'><span>";
           var html_after = "</span><button id='pop-but'>ok</button></p></div>";
           var already = "cmdb主机管理系统已经初始化,无需再次操作";
            success = "cmdb主机管理系统初始化完成";
            $('.overlay').remove();
            var flag = ''
            if (msg['check'] == 0){
                $('body').append(html_before + already + html_after);
                var flag = 'already';
            }else{
                $('body').append(html_before + success + html_after);
                var flag = 'success';
            }
            $('#pop-but').click(function(){
                $('.overlay').remove()
                if(flag == 'success'){
                    window.location.reload();
                }

            })


            // todo 2. 完成初始化后需要记录初始化相关信息
        }

    });
})

/****
 * 刷新主机
 */
$('#host-refurbish').click(function(){
    updateinfo('正在更新');
    $.ajax({
        type: 'POST',
        url: '/host_refurbish',
        data: {'name':'host_refurbish','csrfmiddlewaretoken': jscsrftoken },
        datatype: 'json',
        success: function(msg){ //todo 返回错误信息
            html_before = "<div class='overlay'><p class='popup'><span>";
            html_after = "</span><button id='pop-but'>ok</button></p></div>";
            var data = ' 更新完成 '
            var success = "cmdb主机管理系统初始化完成";
            $('.overlay').remove();
            $('body').append(html_before + data + html_after);

            $('#pop-but').click(function(){
                $('.overlay').remove();
                window.location.reload();
                // if(flag == 'success'){
                //     window.location.reload();
                // }
            })
        }
    })
})